package com.xvheng.module.merchantApi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xvheng.common.annotation.Log;
import com.xvheng.common.core.controller.BaseController;
import com.xvheng.common.core.domain.AjaxResult;
import com.xvheng.common.enums.BusinessType;
import com.xvheng.module.merchantApi.domain.Merchant;
import com.xvheng.module.merchantApi.service.IMerchantService;
import com.xvheng.common.utils.poi.ExcelUtil;
import com.xvheng.common.core.page.TableDataInfo;

/**
 * 商户Controller
 * 商户为店铺的组织
 * @author xvheng
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/merchantApi/merchant")
public class BackendMerchantController extends BaseController
{
    @Autowired
    private IMerchantService merchantService;

    /**
     * 查询商户列表
     */
    @PreAuthorize("@ss.hasPermi('merchantApi:merchant:list')")
    @GetMapping("/list")
    public TableDataInfo list(Merchant merchant)
    {
        startPage();
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        return getDataTable(list);
    }

    /**
     * 导出商户列表
     */
    @PreAuthorize("@ss.hasPermi('merchantApi:merchant:export')")
    @Log(title = "商户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Merchant merchant)
    {
        List<Merchant> list = merchantService.selectMerchantList(merchant);
        ExcelUtil<Merchant> util = new ExcelUtil<Merchant>(Merchant.class);
        util.exportExcel(response, list, "商户数据");
    }

    /**
     * 获取商户详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchantApi:merchant:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(merchantService.selectMerchantByID(ID));
    }

    /**
     * 新增商户
     */
    @PreAuthorize("@ss.hasPermi('merchantApi:merchant:add')")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Merchant merchant)
    {
        return toAjax(merchantService.insertMerchant(merchant));
    }

    /**
     * 修改商户
     */
    @PreAuthorize("@ss.hasPermi('merchantApi:merchant:edit')")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Merchant merchant)
    {
        return toAjax(merchantService.updateMerchant(merchant));
    }

    /**
     * 删除商户
     */
    @PreAuthorize("@ss.hasPermi('merchantApi:merchant:remove')")
    @Log(title = "商户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(merchantService.deleteMerchantByIDs(IDs));
    }
}
